package com.atguigu.book.pojo;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: Cart
 * @Description: 购物车
 * @Author: wty
 * @Date: 2022/12/13
 */

public class Cart {
    /**
     * 购物车中购物车项的集合
     * key是book的bookId,所以用Integer
     */
    private Map<Integer, CartItem> map;
    /**
     * 购物车的总金额
     */
    private Double totalMoney;
    /**
     * 购物车中购物项的数量
     */
    private Integer totalCount;

    /**
     * 购物车中书本的总数量
     */
    private Integer totalBookCount;

    public Cart() {
    }

    public Map<Integer, CartItem> getMap() {
        return map;
    }

    public Integer getTotalBookCount() {
        totalBookCount = 0;
        if (null != map && map.size() > 0) {
            for (CartItem cartItem : map.values()) {
                totalBookCount += cartItem.getBuyCount();
            }
        }
        return totalBookCount;
    }

    public void setTotalBookCount(Integer totalBookCount) {
        this.totalBookCount = totalBookCount;
    }

    public void setMap(Map<Integer, CartItem> map) {
        this.map = map;
    }

    public Double getTotalMoney() {
        // 声明一个变量用于存放累加结果
        BigDecimal totalMoneyBigDecimal = new BigDecimal("0.00");
        if (null != map && map.size() > 0) {
            Set<Map.Entry<Integer, CartItem>> entries = map.entrySet();
            for (Map.Entry<Integer, CartItem> entry : entries) {
                CartItem cartItem = entry.getValue();

                BigDecimal priceBigDecimal = new BigDecimal(cartItem.getBook().getPrice() + "");
                BigDecimal countBigDecimal = new BigDecimal(cartItem.getBuyCount() + "");

                totalMoneyBigDecimal = totalMoneyBigDecimal.add(priceBigDecimal.multiply(countBigDecimal));
                //totalMoney += (Double) (cartItem.getBook().getPrice() * cartItem.getBuyCount());
            }
        }
        totalMoney = totalMoneyBigDecimal.doubleValue();
        return totalMoney;
    }

    public void setTotalMoney(Double totalMoney) {
        this.totalMoney = totalMoney;
    }

    public Integer getTotalCount() {
        totalCount = 0;
        if (null != map && map.size() > 0) {
            totalCount = map.size();
        }
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }
}
